<template>
	<view class="container">
		<view class="m-title">
			收件信息<text @click="copy">点击复制</text>
		</view>
		<view class="main">
			<view class="p">
				<text>收件人</text>{{receive_name}}
			</view>
			<view class="p">
				<text>联系电话</text>{{receive_mobile}}
			</view>
			<view class="p">
				<text>收件地址</text>{{receive_address}}
			</view>
		</view>
		<view class="m-title">
			商品信息
		</view>
		<view class="list-goods">
			<view class="item u-flex u-col-top u-row-between" v-for="(item,index) in list" :key="index">
				<view class="img">
					<image :src="http + item.image" mode="aspectFill"></image>
				</view>
				<view class="con">
					<view class="title u-line-1">
						{{item.title}}
					</view>
					<view class="tips">
						<text v-for="(item2,index2) in item.spec" :key="index2">{{item2}}</text>
						<text v-for="(item2,index2) in item.mountings_text" :key="index2">{{item2.title}}-{{item2.intro}}</text>
					</view>
				</view>
				<view class="r">
					<view class="price">
						{{item.price - item.mountings_price}}元
					</view>
					<view class="num">
						×{{item.num}}
					</view>
				</view>
			</view>
			<view class="total">
				<text>商品金额：¥{{total1(list)}}</text>
				<text>服务费：¥{{total2(list)}}</text>
				<text>合计：¥{{total3(list)}}</text>
			</view>
		</view>
		<view class="m-title">
			收款信息
		</view>

		<view class="list">
			<view class="alpay u-flex u-col-center u-row-between ">
				<view class="l u-flex u-col-center">
					<image src="../../static/image/alpay.png" mode="aspectFill"></image>
					<text>支付宝收款</text>
				</view>
				<view class="r">
					<image src="../../static/image/c3.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					真实姓名
				</view>
				<view class="r">
					<input type="text" v-model="name" placeholder="请输入真实姓名">
				</view>
			</view>
			<view class="item u-flex u-col-center u-row-between">
				<view class="l">
					支付宝账号
				</view>
				<view class="r">
					<input type="text" v-model="alplay" placeholder="请输入支付宝账号">
				</view>
			</view>
		</view>
		<view class="m-title" v-if="hastb">
			淘宝订单号
		</view>
		<view class="textarea" v-if="hastb">
			<textarea v-model="orders" password="多个订单号换行填写"></textarea>
		</view>

		<view class="m-title">
			备注
		</view>
		<view class="textarea">
			<textarea v-model="notes" password="请填写备注"></textarea>
		</view>
		<view class="m-title">
			请仔细阅读回收说明
		</view>
		<view class="body">
			<u-parse :content="content"></u-parse>
		</view>
		<view class="footer">
			<view class="btn" @click="submit">
				下单
			</view>
		</view>
		
		
		<u-popup :show="show" :round="10" mode="center" closeable @close='show=false'>
			<view class="pop">
				<view class="title">歇业公告</view>
				<view style="overflow-y: auto;height: 300rpx;" class="" v-html="ggdata.xieye_remark">
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				http: this.$imgUrl,
				name: "",
				alplay: "",
				orders: "",
				notes: "",
				list: [],
				content: ``,
				hastb: true,
				receive_address: "",
				receive_mobile: '',
				receive_name: '',
				
				show:false,
				ggdata:{
				}
			}
		},
		onLoad() {
			my.setNavigationBar({
				frontColor: '#000',
				backgroundColor: '#fff',
			})
			this.list = uni.getStorageSync('cart2') || []
			this.hastb = this.list.some(item => item.type == 1)
			this.systemConfig()
			this.getgg()

		},
		computed: {
			total1() {
				return (list) => {
					return list.reduce((acc, cur) => acc + parseInt(cur.num) * parseFloat(cur.price), 0).toFixed(2);
				}
			},
			total2() {
				return (list) => {
					return list.reduce((acc, cur) => acc + parseInt(cur.num) * parseFloat(cur.deduct_price), 0)
						.toFixed(2);
				}
			},
			total3() {
				return (list) => {
					let mountings_price = list.reduce((acc, cur) => acc + parseInt(cur.num) * parseFloat(cur.mountings_price),0)
					console.log(mountings_price);
					let a= list.reduce((acc, cur) => acc + parseInt(cur.num) * (parseFloat(cur.price) - parseFloat(cur.deduct_price)), 0)
					return (a-mountings_price).toFixed(2);
				}
			},

		},
		methods: {
			getgg(){
				//是否歇业：0=否,1=是
				this.$http.post('/api/index/xieyeConfig').then(res=>{
					this.ggdata = res.data
				})
			},
			copy() {
				let data = `${this.receive_name}  ${this.receive_mobile}  ${this.receive_address}`
				uni.setClipboardData({
					data: data,
					success: () => {
						console.log('success', data);
						uni.showToast({
							title: '已复制',
							icon: "none"
						});
					}
				});
			},
			systemConfig() {
				this.$http.post('/api/index/systemConfig', {

				}).then(res => {
					if (res.code == 1) {
						this.content = res.data.receive_info
						this.receive_address = res.data.receive_address
						this.receive_mobile = res.data.receive_mobile
						this.receive_name = res.data.receive_name

					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						});
					}
				}).catch(err => {
					console.log('错误信息：', err);
				})
			},
			submit() {
				if(this.ggdata.xieye_switch == '1'){
					this.show = true
					return
				}
				
				if (this.list.length == 0) {
					return
				}
			
				
				if (!this.name || !this.alplay) {
					uni.showToast({
						title: '姓名和支付宝账号不能为空',
						icon: "none"
					});
					return
				}
				let order_price = this.list.reduce((acc, cur) => acc + parseInt(cur.num) * parseFloat(cur.price), 0)
					.toFixed(2);
				let deduct_price = this.list.reduce((acc, cur) => acc + parseInt(cur.num) * parseFloat(cur.deduct_price),
					0).toFixed(2);
					let mountings_price = this.list.reduce((acc, cur) => acc + parseInt(cur.num) * parseFloat(cur.mountings_price),
							0).toFixed(2);
				let products = this.list.map(item => {
					return {
						id: item.id,
						num: item.num,
						type: item.type,
						spec: item.spec.toString(),
						price: item.price,
						mountings_price:item.mountings_price,
						mountings_text:item.mountings_text,
						my_dec_price:item.my_dec_price,
						ta_dec_price:item.ta_dec_price
					}
				})
				uni.showModal({
					title: '温馨提示',
					content: '确定要下单吗?',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/api/order/createOrder', {
					         mountings_price:mountings_price,
								order_price: order_price,
								deduct_price: deduct_price,
								tb_order_ons: this.orders,
								receive_name: this.name,
								receive_account: this.alplay,
								remark: this.notes,
								products: products
							}).then(res => {
								if (res.code == 1) {
									uni.showToast({
										title: res.msg,
										icon: "none"
									});
									uni.removeStorageSync('cart')
									uni.removeStorageSync('cart2')
									this.list = []
									setTimeout(() => {
										uni.redirectTo({
											url: '/pages/index/listOrder'
										})
									}, 1500)
								} else {
									uni.showToast({
										title: res.msg,
										icon: "none"
									});
								}
							}).catch(err => {
								console.log('错误信息：', err);
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
		}
	}
</script>

<style lang="less" scoped>
	.pop{
		width: 630rpx;
		height: 431rpx;
		background: #FCFDFF;
		border-radius: 40rpx;
		padding: 40rpx;
		box-sizing: border-box;
		.title{
			font-size: 40rpx;
			font-weight: bold;
			color: #000;
			text-align: center;
		}
	}
	@color: #E31D21;

	.alpay {
		display: flex;
		align-items: center;
		height: 60rpx;
		line-height: 60rpx;

		.l {
			image {
				width: 60rpx;
				height: 60rpx;
			}

			text {
				font-size: 30rpx;
				color: #333;
				margin-left: 10rpx;
			}
		}

		.r {
			width: 48rpx;
			height: 48rpx;

			image {
				width: 48rpx;
				height: 48rpx;
			}
		}
	}

	.container {
		padding: 30rpx 30rpx 200rpx;

		.footer {
			position: fixed;
			background-color: #fff;
			left: 0;
			right: 0;
			padding: 20rpx 30rpx;
			z-index: 999;
			bottom: 0;

			.btn {
				width: 100%;
				height: 80rpx;
				text-align: center;
				border-radius: 40rpx;
				line-height: 80rpx;
				font-size: 32rpx;
				font-weight: 600;
				color: #fff;
				background-color: @color;
			}
		}

		.body {
			background-color: #fff;
			padding: 20rpx;
			border-radius: 10rpx;
		}

		.textarea {
			background-color: #fff;
			padding: 20rpx;
			border-radius: 10rpx;

			textarea {
				width: 100%;
				height: 100rpx;
				font-size: 28rpx;
				color: #333;
			}
		}

		.list {
			background-color: #fff;
			padding: 20rpx;
			border-radius: 10rpx;

			.item {
				padding: 10rpx 0;

				.l {
					width: 160rpx;
					font-size: 26rpx;
					color: #333;
				}

				.r {
					flex: 1;
					width: 1rpx;
					margin-left: 20rpx;

					input {
						font-size: 28rpx;
						color: #333;
						height: 60rpx;
						line-height: 60rpx;
						width: 100%;
					}
				}
			}
		}

		.list-goods {
			background-color: #fff;
			padding: 20rpx;
			border-radius: 10rpx;

			.total {
				margin-top: 20rpx;
				height: 60rpx;
				line-height: 60rpx;
				font-size: 24rpx;
				text-align: right;
				color: #666;
				white-space: nowrap;

				text {
					margin-left: 20rpx;
				}
			}

			.item {
				padding: 10rpx 0;
				border-bottom: 2rpx solid #eee;

				.img {
					width: 112rpx;
					height: 180rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.con {
					flex: 1;
					padding: 0 20rpx;
					width: 1rpx;

					.title {
						font-size: 28rpx;
						color: #333;
					}

					.tips {

						text {
							color: #999;
							font-size: 24rpx;
							margin: 10rpx 10rpx 0 0;
						}
					}
				}

				.r {
					text-align: right;

					.price {
						font-size: 24rpx;
						color: #666;
						height: 60rpx;
						line-height: 60rpx;
					}

					.num {
						font-size: 24rpx;
						color: #666;
					}
				}
			}
		}

		.m-title {
			height: 80rpx;
			line-height: 80rpx;
			color: #333;
			font-size: 32rpx;

			text {
				margin-left: 10rpx;
				font-size: 24rpx;
				color: @color;
			}
		}

		.main {
			background-color: #fff;
			padding: 20rpx;
			border-radius: 10rpx;

			.p {
				line-height: 40rpx;
				font-size: 24rpx;
				color: #333;

				text {
					width: 150rpx;
					margin-right: 30rpx;
				}
			}
		}
	}
</style>